//
//  XYAlertView.m
//  NemoSDKDemo
//
//  Created by 高超 on 2020/8/26.
//  Copyright © 2020 JackYang. All rights reserved.
//

#import "XYAlertView.h"
#import "UIColor+XYColor.h"
#import "XYPasswordView.h"

@interface XYAlertView ()<XYPasswordViewDelegate>

@property (nonatomic, strong) UIView *bgView;

@property (nonatomic, assign) XYAlertType type;

@property (nonatomic, strong) UIViewController *vc;

@end

@implementation XYAlertView

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor colorWithWhite:0 alpha:0.6];
    
    self.bgView = [[UIView alloc] initWithFrame:CGRectMake((kScreen.width-288)/2, (kScreen.height-140)/2, 288, 140)];
    self.bgView.backgroundColor = [UIColor whiteColor];
    self.bgView.layer.cornerRadius = 10.f;
    [self.view addSubview:self.bgView];
    
    [self _setupView];
}

+ (XYAlertView *)alertType:(XYAlertType)type {
    XYAlertView *alert = [[XYAlertView alloc] init];
    alert.type = type;
    return alert;  
}

- (void)show:(UIViewController *)vc {
    [vc addChildViewController:self];
    [vc.view addSubview:self.view];
    self.view.frame = vc.view.bounds;
    [self didMoveToParentViewController:vc];
}

- (void)remove {
    [self willMoveToParentViewController:nil];
    [self removeFromParentViewController];
    [self.view removeFromSuperview];
}

- (void)_setupView {
    for (UIView *subview in self.bgView.subviews) {
        [subview removeFromSuperview];
    }
    if (_type == XYAlertTypeCreateMeeting) {
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 24, 248, 40)];
        titleLabel.text = @"已为您创建随机会议号，请在24小时内使用";
        titleLabel.textColor = [UIColor colorWithHexString:@"#393946" withAlpha:0.8];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.numberOfLines = 0;
        titleLabel.font = [UIFont systemFontOfSize:14];
        [self.bgView addSubview:titleLabel];
        
        UIButton *okBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        okBtn.frame = CGRectMake(60, CGRectGetMaxY(titleLabel.frame)+16, 168, 44);
        okBtn.backgroundColor = [UIColor colorWithHexString:@"#588DF7" withAlpha:0.9];
        [okBtn setTitle:@"我知道了" forState:UIControlStateNormal];
        [okBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        okBtn.titleLabel.font = [UIFont systemFontOfSize:16];
        okBtn.layer.cornerRadius = 22.f;
        [okBtn addTarget:self action:@selector(okBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.bgView addSubview:okBtn];
    }
    
    if (_type == XYAlertTypePassword) {
        self.bgView.xy_y -= 80;
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 18, 128, 24)];
        titleLabel.text = @"请输入密码";
        titleLabel.textColor = [UIColor colorWithHexString:@"#393946"];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.font = [UIFont systemFontOfSize:17];
        [self.bgView addSubview:titleLabel];
        
        UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        cancelBtn.frame = CGRectMake(252, 12, 24, 24);
        [cancelBtn setImage:[UIImage imageNamed:@"password_cancel"] forState:UIControlStateNormal];
        [cancelBtn addTarget:self action:@selector(remove) forControlEvents:UIControlEventTouchUpInside];
        [self.bgView addSubview:cancelBtn];
        
        XYPasswordView *pv = [[XYPasswordView alloc] initWithFrame:CGRectMake(24, CGRectGetMaxY(titleLabel.frame)+18, 240, 40)];
        pv.delegate = self;
        [self.bgView addSubview:pv];
    }
}

- (void)okBtnClick:(UIButton *)sender {
    [self remove];
}

#pragma mark- XYPasswordViewDelegate
- (void)passWordDidChange:(XYPasswordView *)password{
    
}
-(void)passWordCompleteInput:(XYPasswordView *)password{
    if (self.inputText) {
        self.inputText(password.textStore);
    }
    [password resignFirstResponder];
    [self remove];
}
- (void)passWordBeginInput:(nonnull XYPasswordView *)password {
    
}

@end
